package ferp.android.dialogs;

import android.content.Context;
import android.view.View;
import android.widget.TextView;
import ferp.android.GUI;
import ferp.android.R;
import ferp.android.managers.ProfileManager;
import ferp.android.views.LastTrickView;
import ferp.core.card.Card;
import ferp.core.game.Game;
import ferp.core.game.Trick;
import ferp.core.player.Profile;

/**
 * User: olegs
 * Date: 10/11/12 10:31 PM
 */
public class LastTrickDialog extends Dialog
{
  public static LastTrickDialog create(Context context)
  {
    Profile profile = ProfileManager.instance(context).getCurrentProfile();

    if (profile == null)
      return null;

    Game game = profile.game();

    return game == null || game.trick.last == 0 ? null : new LastTrickDialog(context);
  }

  public void prepare()
  {
    Profile profile = ProfileManager.instance(getContext()).getCurrentProfile();
    int trick = profile.game().trick.last;
    Card.Suit trump = profile.game().trump();

    LastTrickView v = (LastTrickView)findViewById(R.id.dlg_last_trick_view);
    v.show(profile.settings, trick);

    byte winner = Trick.winner(trick, trump);

    switch (winner)
    {
      case 0:
        note.setText(R.string.last_trick_human);
        break;

      case 1:
        note.setText(R.string.last_trick_left_player);
        break;

      case 2:
        note.setText(R.string.last_trick_right_player);
        break;
    }
  }

  private LastTrickDialog(Context context)
  {
    super(context, R.layout.dialog_last_trick, R.string.last_trick);

    setCancelable(true);

    note = (TextView)findViewById(R.id.dlg_last_trick_text);
    note.setTypeface(GUI.Font.BOLD);

    findViewById(R.id.dlg_last_trick_layout).setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View view)
      {
        LastTrickDialog.this.dismiss();
      }
    });
  }

  private TextView note;
}
